package org.dizem.groupon.dao.impl;

import org.dizem.groupon.dao.MerchantDAO;
import org.dizem.groupon.model.domain.MerchantEntity;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import java.util.List;

/**
 * User: dizem@126.com
 * Time: 11-6-26 下午8:07
 */
public class MerchantDAOImpl extends DAOImplSupport<MerchantEntity> implements MerchantDAO {
	public MerchantEntity getById(int id) {
		return (MerchantEntity) getHibernateTemplate().get(MerchantEntity.class, id);
	}

	public List<MerchantEntity> loadAll() {
		return getHibernateTemplate().loadAll(MerchantEntity.class);
	}

	public List<MerchantEntity> load(int firstResult, int maxResult) {
		return getHibernateTemplate().findByCriteria(DetachedCriteria.forClass(MerchantEntity.class), firstResult, maxResult);
	}

	public MerchantEntity getByName(String merchantName) {
		DetachedCriteria c = DetachedCriteria.forClass(MerchantEntity.class);
		c.add(Restrictions.eq("name", merchantName));
		List list = getHibernateTemplate().findByCriteria(c);
		return list.size() == 0 ? null : (MerchantEntity) list.get(0);
	}
}
